<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class HomeForm extends CFormModel {

    public $recipients;
    public $province;
    public $city;
    public $district;
    public $streetDetail;
    public $mobile;
    public $postalcode;
    public $areacode;
    public $phonenum;
    public $extension;
    public $area;
    public $street;
    public $oldpassword;
    public $newpassword;
    public $newpassword_repeat;
    public $default;
    public $rememberMe = false;
    private $_identity;
    public $errorCode;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            array('mobile, streetDetail,postalcode,recipients,area', 'required', 'on' => 'address'),
            array('oldpassword, newpassword,newpassword_repeat', 'required', 'on' => 'security'),
            array('newpassword_repeat', 'compare', 'on' => 'security', 'compareAttribute' => 'newpassword', 'message' => '两次密码不一致'),
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'recipients' => '收货人姓名',
            'area' => '所在地区',
            'postalcode' => '邮政编码',
            'telephone' => '座机号码',
            'road' => '街道地址',
            'streetDetail' => '详细地址',
            'mobile' => '手机号码',
            'oldpassword' => '旧密码',
            'newpassword' => '新密码',
            'newpassword_repeat' => '确认新密码',
            'default' => '是否设置为默认收货地址'
        );
    }

    public function updatePassword() {
        if ($this->_identity === null) {
            $api = new Api('account');
            $userid = Yii::app()->user->id;
            $oldPassWord = trim($_POST['HomeForm']['oldpassword']);
            $passWord = trim($_POST['HomeForm']['newpassword']);
            $confirmPassWord = trim($_POST['HomeForm']['newpassword_repeat']);
            $params = array(
                'UpdatePwdByPwdReq' => array(
                    'userId' => $userid,
                    'oldPassword' => $oldPassWord,
                    'password' => $passWord,
                    'confirmPassword' => $confirmPassWord,
                )
            );
            if (!$oldPassWord) {
                $this->addError('oldpassword', '旧密码不能为空');
            } elseif (!$passWord) {
                $this->addError('newpassword', '新密码不能为空');
            } elseif (!$confirmPassWord) {
                $this->addError('newpassword_repeat', '确认密码不能为空');
            } else {
                if ($passWord == $confirmPassWord) {
                    $row = $api->apiclient('/LdAccountServer/userAccount/updatePwdByPwd', $params);
                    if ($row == 0) {
                        throw new CHttpException(404, '网络异常,请稍后再试');
                    }
                    if ($row['code'] == 0) {
                        return true;
                    } else {
                        $this->addError('oldpassword', $row['ExceptionRsp']['msg']);
                    }
                } else {
                    $this->addError('newpassword', '两次密码不一致');
                }
            }
        }
        return false;
    }

}
